<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>进货管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="favicon.ico">
    <link rel="stylesheet" href="/layuicms/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/layuicms/css/public.css" media="all" />
</head>
<body class="childrenBody">
<!-- 查询条件开始 -->
<!--    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
        <legend>查询条件</legend>
    </fieldset>-->
<blockquote class="layui-elem-quote" style="height: 80px;">
    <form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">供应商</label>
                <div class="layui-input-inline">
                    <select name="providerid" id="search_provider">
                        <option value="0">请选择供应商</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-block">
                    <select name="goodsid" id="search_goodsid">
                        <option value="0">请选择商品</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">开始时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="startTime" readonly="readonly" id="startTime"  autocomplete="off"  class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">结束时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="endTime" id="endTime" readonly="readonly" autocomplete="off"  class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-block" style="text-align: center;">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="doSearch"> <span class="layui-icon layui-icon-search"></span> 查询</button>
                    <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span> 重置</button>
                </div>
            </div>
        </div>
    </form>
</blockquote>
<!--查询条件结束-->
<!--数据表格开始-->
<div>

    <table class="layui-hide" id="inportTable" lay-filter="inportTable"></table>

    <div id="inportToolBar" style="display: none ;"  >
        <button type="button" lay-event="add" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-add-circle"></span> 添加进货</button>
        <button type="button" lay-event="batchDelete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span> 批量删除</button>
    </div>
    <div id="inportRowBar" style="display: none">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-edit"></span> 修改</button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span> 删除</button>
        <button type="button" lay-event="back" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span> 退货</button>        </div>
</div>
</div>
<!--数据表格结束-->

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form action="" method="post" class="layui-form" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">供应商</label>
                <div class="layui-input-inline">
                    <select name="providerid" id="providerid"   lay-filter="providerid">
                        <option value="0">请选择供应商</option>
                    </select>
                    <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <select name="goodsid" id="goodsid">
                        <option value="0">请选择商品</option>
                    </select>
                    <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">进货数量</label>
                    <div class="layui-input-inline">
                        <input type="hidden" name="id">
                        <input type="text" class="layui-input"  name="number" lay-verify="number" placeholder="请输入商品进货包装">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">进货价格</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="inportprice" lay-verify="number" placeholder="请输入商品进货规格">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">支付类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="paytype" value="支付宝" title="支付宝">
                    <input type="radio" name="paytype" value="微信" title="微信">
                    <input type="radio" name="paytype" value="信用卡" title="信用卡">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">进货备注</label>
                <div class="layui-input-block">
                    <textarea  name="remark" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block" style="text-align: center;">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="doSubmit" id="doSubmit" ><span class="layui-icon layui-icon-add-1"></span>提交</button>
                    <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
                </div>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->


<!-- 退货的弹出层  开始 -->
<div id="backGoodsDiv" style="display: none;padding: 5px">
    <form action="" method="post" class="layui-form" id="dataBackFrm" lay-filter="dataBackFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">退货数量</label>
            <div class="layui-input-block">
                <input type="hidden" name="id"> <!-- 进货单ID -->
                <input type="hidden" name="currentnumber" id="currentnumber"> <!-- 当前进货单的数量 -->
                <input type="text" class="layui-input"  name="number" lay-verify="required|checknumber" placeholder="请输入退货数量">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">退货备注</label>
            <div class="layui-input-block">
                <textarea  name="remark" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="doBackSubmit" id="doBackSubmit" ><span class="layui-icon layui-icon-add-1"></span>提交</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>
<!-- 退货的弹出层  结束 -->

</body>
<script type="text/javascript" src="/layuicms/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['jquery','form','table','layer','laydate'],function(){
        var $ = layui.jquery;
        var form=layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var upload = layui.upload;
        var laydate = layui.laydate;
        //初始化日期
        laydate.render({
            elem:'#startTime',
            type:'datetime'
        });
        laydate.render({
            elem:'#endTime',
            type:'datetime'
        })
        //加载数据
        var tableIns = table.render({
            elem: '#inportTable'
            ,url:'/inport/loadAllInport'
            ,toolbar: '#inportToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '用户登录进货数据表'
            ,height:'full-140'
            ,page: true
            ,cols: [ [
                {type:'checkbox',align:'center'},
                {field:'id', title:'进货ID',align:'center' ,width:'80'}
                ,{field:'providername', title:'供应商',align:'center',width:'120'}
                ,{field:'goodsname', title:'商品名称',align:'center',width:'120'}
                ,{field:'size', title:'商品规格',align:'center',width:'120'}
                ,{field:'inporttime', title:'进货时间',align:'center',width:'180'}
                ,{field:'number', title:'进货数量',align:'center',width:'100'}
                ,{field:'inportprice', title:'进货价格',align:'center',width:'100'}
                ,{field:'remark', title:'备注',align:'center',width:'120'}
                ,{field:'operateperson', title:'操作员',align:'center',width:'100'}
                ,{fixed: 'right', title:'操作', toolbar: '#inportRowBar',align:'center',width:'280'}
            ] ]
            ,done: function(res, curr, count){ //处理删除某一页最后一条数据的BUG
                if(res.data.length==0&&curr!=1){
                    tableIns.reload({
                        page:{
                            curr:(curr-1)
                        }
                    });
                }
            }
        });
        //加载查询条件供应商的下拉列表
        $.get("/provider/loadAllProviderForSelect",function(res){
            var data=res.data;
            var dom=$("#search_provider");
            var html='<option value="0">请选择供应商</option>'
            $.each(data,function(index,item){
                html+='<option value="'+item.id+'">'+item.providername+'</option>'
            });
            dom.html(html);
            form.render("select");
        });
        //加载查询条件商品的下拉列表
        $.get("/goods/loadAllGoodsForSelect",function(res){
            var data=res.data;
            var dom=$("#search_goodsid");
            var html='<option value="0">请选择商品</option>'
            $.each(data,function(index,item){
                html+='<option value="'+item.id+'">'+item.goodsname+'-'+item.size+'['+item.providername+']</option>'
            });
            dom.html(html);
            form.render("select");
        });
        //加载查询条件供应商的下拉列表
       /* $.get("/provider/loadAllProviderForSelect",function (res) {
            alert(res.data());
            var  data = res.data;
            var dom= $('#serach_provider');
            var html = '<option value="0">请选择供应商</option>';
            $.each(data,function (index,items) {
                html+='<option value="'+items.id+'>'+items.providername+'</option>';
            })
            dom.html(html);
            form.render("select");
        });*/
        //模糊查询
        form.on("submit(doSearch)",function (data) {
            tableIns.reload({
                where:data.field,
                page:{
                    curr:1
                }
            });
            return false;
        });
        //监听工具条事件
        //监听工具条事件
        table.on('toolbar(inportTable)', function(obj){
            switch(obj.event){
                case 'batchDelete':
                    batchDelete();
                    break;
                case 'add':
                    openAddLayer();
                    break;
            };
        });
        //监听行工具条事件
        table.on('tool(inportTable)', function(obj){
            var data = obj.data; //获得当前行数据
            switch(obj.event){
                case 'update':
                    openUpdateLayer(data);
                    break;
                case 'back':
                    back(data);
                    break;
                case 'delete':
                    deleteInport(data);
                    break;
            };
        });
        var mainIndex;
        var url;
        //打开添加的弹出层
        function openAddLayer() {
            mainIndex = layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['800px','500px'],
                title:'添加进货',
                success:function () {
                    $("#dataFrm")[0].reset();
                    url="/inport/addInport"
                    initProviderSelect();
                    $(".mydiv").hide();
                }
            });
        }
        //初始化添加和修改页面的下拉列表
        function initProviderSelect(providerid){
            var dom = $("#goodsid");
            dom.html("");
            $.get("/provider/loadAllProviderForSelect",function(res){
                var data=res.data;
                var dom=$("#providerid");
                var html='<option value="0">请选择供应商</option>'
                $.each(data,function(index,item){
                    html+='<option value="'+item.id+'">'+item.providername+'</option>'
                });
                dom.html(html);
                //如果providerid有值就反选
                if (providerid!=undefined){
                    dom.val(providerid);
                }
                form.render("select");
            });
        }

        //监听添加页面供应商下拉框的改变事件
        form.on('select(providerid)',function (data) {
            var providerid = data.value;
            initGoodsSelect(providerid);
        })
        //加载商品下拉列表
        function initGoodsSelect(providerid,goodsid) {
            $.get("/goods/loadGoodsByProviderid",{providerid:providerid},function(res){
                var data=res.data;
                var dom=$("#goodsid");
                var html='<option value="0">请选择商品</option>'
                $.each(data,function(index,item){
                    html+='<option value="'+item.id+'">'+item.goodsname+'-'+item.size+'['+item.providername+']</option>'
                });
                dom.html(html);
                //如果goodsid有值就反选
                if (goodsid!=undefined){
                    dom.val(goodsid);
                }
                form.render("select");
            });
        }
        //打开修改弹出层
        function openUpdateLayer(data) {
            mainIndex = layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['800px','500px'],
                title:'修改进货',
                success:function () {
                    $("#dataFrm")[0].reset();
                    form.val("dataFrm",data);
                    initProviderSelect(data.providerid);
                    initGoodsSelect(data.providerid,data.goodsid);
                    url="/inport/updateInport";
                    //禁用供应商和商品的下拉框
                    $(".mydiv").show();
                }
            });
        }

        $("#doSubmit").click(function () {
            //同步富文本和textarea里面的内容
            var data = $("#dataFrm").serialize();
            $.post(url,data,function (res) {
                if(res.code==20000){
                    tableIns.reload();
                }
                layer.msg(res.message);
                layer.close(mainIndex);
            })
        });

        //添加和修改的提交
        /*form.on("submit(doSubmit)",function (data) {
            //同步富文本和textarea里面的内容
            layedit.sync(editIndex)
            $.post(url,data.field,function (res) {
                if(res.code==20000){
                    tableIns.reload();
                }
                layer.msg(res.message);
                layer.close(mainIndex);
            })
            return false
        });*/

        //根据ID删除
        function deleteInport(data) {
            layer.confirm('你确定删除这个商品进货吗?', {icon: 3, title:'提示'}, function(index){
                    $.post("/inport/deleteInport",{id:data.id},function (res) {
                        if(res.code==20000){
                            tableIns.reload();
                        }
                        layer.close(res.message);
                    })
                    layer.close(index);
                }
            )};
        //批量删除方法
        function batchDelete() {
            var checkStatus = table.checkStatus('inportTable');
            var dataLength = checkStatus.data.length;
            if(dataLength>0){
                layer.confirm('你确定删除吗?', {icon: 3, title:'提示'}, function(index){
                    //do something
                    var data = checkStatus.data; //获取选中行的数据
                    var ids = "";
                   // var inportimgs = "";
                    $.each(data,function(index,item){
                        if(index==0){
                            ids += "ids="+item.id;
                     //       inportimgs +="inportimgs="+item.inportimg;
                        }else {
                            ids += "&ids="+item.id;
                       //     inportimgs +="&inportimgs="+item.inportimg;
                        }

                    })
                    $.post("/inport/deleteInports",ids,function (res) {
                        if(res.code==20000){
                            tableIns.reload();
                        }
                        layer.close(res.message);
                    })
                    layer.close(index);
                });
            }else{
                layer.msg("请选中操作行");
            }
        }
        //打开退货弹出层
        function back(data) {
            mainIndex = layer.open({
                type:1,
                content:$("#backGoodsDiv"),
                area:['800px','500px'],
                title:'商品退货',
                success:function () {
                    $("#dataBackFrm")[0].reset();
                    form.val("dataBackFrm",{id:data.id,currentnumber:data.number});
                }
            });
        }

        //自定义验证规则
        form.verify({
            checknumber: function(value){
              var number = parseInt($("#currentnumber").val());
                if(parseInt(value) > number || parseInt(value)<1){
                    return '退货数量不能大于退货单商品数量【'+$("#currentnumber").val()+"】";
                }
            }
        });

        //退货提交
        form.on("submit(doBackSubmit)",function (data) {
            $.post("/outport/addOutport",data.field,function (res) {
               if (res.code==20000){
                   tableIns.reload();
               }
               layer.msg(res.message);
               layer.close(mainIndex);
            });
            return false;
        })
    });
</script>
</html>